/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import bean.managed.util.JsfUtil;
import entity.BillOfMaterialEntity;
import entity.BomRawMaterialWeightEntity;
import entity.CompletedGoodsEntity;
import entity.DataObjectEntity;
import entity.LocationQuantityEntity;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author wei
 */
@Stateless
public class CompletedGoodsEntityFacade extends AbstractFacade<CompletedGoodsEntity> {
    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

//     public DataModel findAllCGInLocation(Long cgLocationID) {
//      CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
//            javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
//            Root<DataObjectEntity> locationQuantityRoot = cq.from(LocationQuantityEntity.class);
//            cq.select(locationQuantityRoot);
//            cq.where(builder.equal(locationQuantityRoot.get("location").get("id"), cgLocationID));
//            return new ListDataModel(getEntityManager().createQuery(cq).getResultList());
//    }
    
    public CompletedGoodsEntity findCGByName(String productName){
         CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
            javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
            Root<DataObjectEntity> completedGoodsRoot = cq.from(CompletedGoodsEntity.class);
            cq.select(completedGoodsRoot);
            cq.where(builder.equal(completedGoodsRoot.get("productName"), productName));
            List<CompletedGoodsEntity> tempList = new ArrayList<CompletedGoodsEntity>(getEntityManager().createQuery(cq).getResultList());
            System.out.println(tempList.get(0).getProductName());
            return tempList.get(0);
    }
    
     public List findAllCG() {
      CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
            javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
            Root<DataObjectEntity> locationQuantityRoot = cq.from(LocationQuantityEntity.class);
            cq.select(locationQuantityRoot);
            cq.where(builder.equal(locationQuantityRoot.get("archived"), false));
            return new ArrayList(getEntityManager().createQuery(cq).getResultList());
    }
//     
//     public List findAllExpiringCG() {
//      CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
//            javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
//            Root<DataObjectEntity> locationQuantityRoot = cq.from(LocationQuantityEntity.class);
//            cq.select(locationQuantityRoot);
//            //cq.where(builder.equal(locationQuantityRoot.get("location").get("type"), "CG"));
//            return new ArrayList(getEntityManager().createQuery(cq).getResultList());
//    }
     
      public List findAllCGInLocation(Long cgLocationID) {
      CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
            javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
            Root<DataObjectEntity> locationQuantityRoot = cq.from(LocationQuantityEntity.class);
        
            Predicate predicate1 = builder.equal(locationQuantityRoot.get("location").get("id"), cgLocationID);
            Predicate predicate2 = builder.equal(locationQuantityRoot.get("archived"), false);
            
               cq.select(locationQuantityRoot);
               cq.where(builder.and(predicate1, predicate2));
            return new ArrayList(getEntityManager().createQuery(cq).getResultList());
    }
     
//    
//        public DataModel findAllCompletedGoodsInLocation() {
//        Query query = em.createQuery("SELECT L FROM LocationToQuantity L where L.location.id="+"");
//        return new ListDataModel(query.getResultList());
//    }
     
    public CompletedGoodsEntityFacade() {
        super(CompletedGoodsEntity.class);
    }

    public void createCompletedGood(CompletedGoodsEntity current, ArrayList<BomRawMaterialWeightEntity> bomMaterialList) {
        double weightPerBar = current.getWeightPerBar();
        int capacityPerMinute = current.getCapacityPerMin();
        current.setCapacityPerHour(capacityPerMinute*60);
        int barsPerBox = current.getBarsPerBox();
        int boxesPerCase = current.getBoxesPerCase();
        current.setCapacityPerHour(capacityPerMinute*60);
        current.setBarsPerCase(barsPerBox*boxesPerCase);
        current.setWeightPerBox(weightPerBar*barsPerBox/1000);//in kg
        current.setWeightPerCase(weightPerBar*current.getBarsPerCase()/1000);//kg
        current.setCasesOfHour(current.getCapacityPerHour()/current.getBarsPerCase());
        
        BillOfMaterialEntity bom = new BillOfMaterialEntity();
        bom.setRawMaterialBreakDows(bomMaterialList);
        em.persist(bom);
        
        current.setBom(bom);
        em.persist(current);
        
        
    }

    public void delete(CompletedGoodsEntity good) {
        
        
        try {
            good.setArchived(true);
            good.getBom().setArchived(true);
            em.merge(good);
            
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("CompletedGoodsEntityDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
        
    }

    public void updateGood(CompletedGoodsEntity current) {
       double weightPerBar = current.getWeightPerBar();
        int capacityPerMinute = current.getCapacityPerMin();
        current.setCapacityPerHour(capacityPerMinute*60);
        int barsPerBox = current.getBarsPerBox();
        int boxesPerCase = current.getBoxesPerCase();
        current.setCapacityPerHour(capacityPerMinute*60);
        current.setBarsPerCase(barsPerBox*boxesPerCase);
        current.setWeightPerBox(weightPerBar*barsPerBox/1000);//in kg
        current.setWeightPerCase(weightPerBar*current.getBarsPerCase()/1000);//kg
        current.setCasesOfHour(current.getCapacityPerHour()/current.getBarsPerCase());
        
        BillOfMaterialEntity bom = current.getBom();
        em.merge(bom);
        em.merge(current);
    }

    public boolean checkUserNameExist(String materialName, List<CompletedGoodsEntity> materials) {
         for(CompletedGoodsEntity r:materials){
            if(r.getProductName().equals(materialName)){
                return true;
            }
        }
        return false;
    }


    
}
    































